from django.db import models


class Drivinginfo(models.Model):
    # blank=True 表单验证时允许为空 null=True 数据空会存在空值
    # 在南昌提供的csv文件中观察得到：所有的字段均有可能为空值
    car_number = models.CharField(max_length=16, blank=True, null=True, help_text='车牌号', verbose_name='车牌号')
    vin = models.CharField(max_length=20, blank=True, null=True, help_text='VIN', verbose_name='VIN')
    data_time = models.DateTimeField(blank=True, null=True, verbose_name='采集时间')
    longitude = models.DecimalField(max_digits=10, decimal_places=6, blank=True, null=True, help_text='经度',
                                    verbose_name='经度')
    latitude = models.DecimalField(max_digits=10, decimal_places=6, blank=True, null=True, help_text='纬度',
                                   verbose_name='纬度')
    # 注释掉的字段对项目的意义不大
    # obd_protocol = models.CharField(max_length=15, blank=True, null=True, verbose_name='OBD诊断协议')
    # mil_status = models.CharField(max_length=5, blank=True, null=True, verbose_name='MIL状态')
    # diagnose_status = models.CharField(max_length=10, blank=True, null=True, verbose_name='诊断支持状态')
    # diagnose_ready_status = models.CharField(max_length=10,blank=True, null=True,verbose_name='诊断就绪状态')
    car_speed = models.DecimalField(max_digits=8, decimal_places=4, blank=True, null=True, help_text='车速',
                                    verbose_name='车速')
    atmospheric_pressure = models.FloatField(blank=True, null=True, help_text='大气压力',
                                             verbose_name='大气压力')
    actual_torque = models.FloatField(blank=True, null=True,
                                      help_text='发动机净输出扭矩或发动机实际扭矩/指示扭矩',
                                      verbose_name='实际扭矩')
    friction_torque = models.FloatField(blank=True, null=True, help_text='摩擦扭矩', verbose_name='摩擦扭矩')
    engine_speed = models.FloatField(blank=True, null=True, help_text='发动机转速',
                                     verbose_name='发动机转速')
    fuel_flow = models.FloatField(blank=True, null=True, help_text='发动机燃料流量',
                                  verbose_name='燃料流量')
    scr_up_nox = models.FloatField(blank=True, null=True, help_text='SCR上游NOx传感器输出值', verbose_name='SCR上游NOx')
    scr_down_nox = models.FloatField(blank=True, null=True, help_text='SCR下游NOx传感器输出值', verbose_name='SCR下游NOx')
    reactive_agent = models.FloatField(blank=True, null=True, help_text='反应剂余量', verbose_name='反应剂余量')
    air_intake = models.FloatField(blank=True, null=True, help_text='进气量',
                                   verbose_name='进气量')
    scr_inlet_temperature = models.FloatField(blank=True, null=True, help_text='SCR入口温度', verbose_name='SCR入口温度')
    scr_outlet_temperature = models.FloatField(blank=True, null=True, help_text='SCR出口温度', verbose_name='SCR出口温度')
    dpf_pressure_difference = models.FloatField(blank=True, null=True,
                                                help_text='DPF压差', verbose_name='DPF压差')
    coolant_temperature = models.FloatField(blank=True, null=True,
                                            help_text='发动机冷却液温度', verbose_name='冷却液温度')
    tank_level = models.FloatField(blank=True, null=True,
                                   help_text='剩余油量', verbose_name='剩余油量')
    accumulation_mile = models.IntegerField(blank=True, null=True,
                                            help_text='累计里程', verbose_name='累计里程')
    location_status = models.CharField(max_length=10, blank=True, null=True,
                                       help_text='定位有效状态', verbose_name='定位有效状态')
    engine_status = models.CharField(max_length=10, blank=True, null=True,
                                     help_text='发动机状态', verbose_name='发动机状态')
    # 续航里程、手刹、车门状态信息意义不大
    fuel_consumption = models.FloatField(blank=True, null=True,
                                         help_text='百公里油耗计算值', verbose_name='百公里油耗计算值')
    fuel_consumption_pre = models.FloatField(blank=True, null=True,
                                             help_text='百公里油耗预测值', verbose_name='百公里油耗预测值')

    class Meta:
        verbose_name = '驾驶信息'
        verbose_name_plural = verbose_name


class BasicInfo(models.Model):
    car_number = models.CharField(max_length=16, blank=True, null=True, help_text='车牌号', verbose_name='车牌号')
    vin = models.CharField(max_length=20, help_text='VIN', verbose_name='VIN', unique=True)
    province = models.CharField(max_length=20, blank=True, null=True, help_text='省份', verbose_name='省份')
    city = models.CharField(max_length=20, blank=True, null=True, help_text='城市', verbose_name='城市')

    class Meta:
        verbose_name = '车辆基础信息'
        verbose_name_plural = verbose_name


class DrivingMode(models.Model):
    basic_info = models.OneToOneField(to=BasicInfo, verbose_name='基础信息', on_delete=models.CASCADE, null=False)
    driving_mode = models.CharField(max_length=5, blank=True, null=True, help_text='驾驶风格', verbose_name='驾驶风格')
    car_speed_mean = models.FloatField(blank=True, null=True, help_text='车速平均值',
                                       verbose_name='车速平均值')
    car_speed_sd = models.FloatField(blank=True, null=True, help_text='车速标准差',
                                     verbose_name='车速标准差')
    acceleration_sd = models.FloatField(blank=True, null=True, help_text='加速度标准差',
                                        verbose_name='加速度标准差')
    overspeed_proportion = models.FloatField(blank=True, null=True, help_text='超速时间比',
                                             verbose_name='超速时间比')
    rapid_acceleration_proportion = models.FloatField(blank=True, null=True,
                                                      help_text='急加速时间比', verbose_name='急加速时间比')
    engine_speed_mean = models.FloatField(blank=True, null=True, help_text='发动机转速平均值',
                                          verbose_name='发动机转速平均值')
    engine_speed_sd = models.FloatField(blank=True, null=True, help_text='发动机转速标准差',
                                        verbose_name='发动机转速标准差')

    class Meta:
        verbose_name = '驾驶风格数据'
        verbose_name_plural = verbose_name


class LogisticsMode(models.Model):
    basic_info = models.OneToOneField(to=BasicInfo, verbose_name='基础信息', on_delete=models.CASCADE)
    logistics_mode = models.CharField(max_length=5, blank=True, null=True, help_text='物流模式', verbose_name='物流模式')
    radius_of_gyration = models.FloatField(help_text='回转半径', verbose_name='回转半径')
    daily_travel_location = models.FloatField(help_text='日均出行地点', verbose_name='日均出行地点')
    motion_entropy = models.FloatField(help_text='活动熵', verbose_name='活动熵')
    daily_driving_distance = models.FloatField(help_text='日均行驶里程', verbose_name='日均行驶里程')
    daily_driving_time = models.FloatField(help_text='日均行驶时间', verbose_name='日均行驶时间')

    class Meta:
        verbose_name = '物流模式数据'
        verbose_name_plural = verbose_name


class DataFields(models.Model):
    field_name = models.CharField(max_length=100, null=False, blank=False, unique=True, verbose_name='字段名称',
                                  help_text='字段名称')
    unit = models.CharField(max_length=10, null=True, blank=True, verbose_name='单位', help_text='单位')

    class Meta:
        verbose_name = '字段基础信息'
        verbose_name_plural = verbose_name


class DataDescription(models.Model):
    field = models.ForeignKey(to=DataFields, on_delete=models.CASCADE)
    basic_info = models.ForeignKey(to=BasicInfo, on_delete=models.CASCADE)
    mean = models.FloatField(null=True, blank=True, verbose_name='平均值')
    median = models.FloatField(null=True, blank=True, verbose_name='中位数')
    count = models.IntegerField(null=True, blank=True, verbose_name='有效数据量')
    refresh_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    year = models.IntegerField(null=False, blank=False, verbose_name='统计年份')
    month = models.IntegerField(null=False, blank=False, verbose_name='统计月份')

    class Meta:
        verbose_name = '字段描述信息'
        verbose_name_plural = verbose_name


class WorkingPlace(models.Model):
    basic_info = models.ForeignKey(to=BasicInfo, on_delete=models.CASCADE)
    longitude = models.DecimalField(max_digits=10, decimal_places=6, help_text='经度',
                                    verbose_name='经度')
    latitude = models.DecimalField(max_digits=10, decimal_places=6, help_text='纬度',
                                   verbose_name='纬度')
    max_word = models.CharField(max_length=10, help_text='高频词汇', verbose_name='高频词汇')
    max_word_num = models.IntegerField(help_text='高频词汇次数', verbose_name='高频词汇次数')
    max_tag = models.CharField(max_length=10, help_text='高频标签', verbose_name='高频标签')
    max_tag_num = models.IntegerField(help_text='高频标签次数', verbose_name='高频标签次数')
    nearest_site = models.CharField(max_length=30, help_text='最近地点', verbose_name='最近地点')
    nearest_site_distance = models.IntegerField(help_text='最近地点距离', verbose_name='最近地点距离')
    refresh_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')


class LoadFile(models.Model):
    uid = models.CharField(max_length=20, help_text='uid', verbose_name='uid')
    file_name = models.CharField(max_length=50, help_text='载荷文件名', verbose_name='载荷文件名')
    upload_time = models.DateTimeField(auto_now_add=True, verbose_name='文件上传时间')
    file_data = models.FileField(upload_to='load_files/')


class LoadPicture(models.Model):
    picture_name = models.CharField(max_length=50, help_text='载荷预测图', verbose_name='载荷预测图')
    upload_time = models.DateTimeField(auto_now_add=True, verbose_name='文件上传时间')
